/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.azon.left.hand;

/**
 *
 * @author Mahdan Ahmad F A
 */
public abstract class Guesser {
    abstract public String doGueess(String chain_code);
    
    public void printObjectProperty(String chain_code) {
        String  code        = getChainCode(chain_code);
        int[]   frequency   = getFrequency(chain_code);
        String  sequence    = getSequence(chain_code);
        
        
        System.out.println("Chain code = " + code);
        
        System.out.println("Frekuensi");
        for (int i = 0; i < frequency.length; i++) {
            System.out.println((i + 1) + " = " + frequency[i]);
        }
        System.out.println("");
        System.out.println("Sequence = " + sequence);
        System.out.println(""); // */
    }
    
    protected String getChainCode(String chain_code) {
        return chain_code.substring(chain_code.indexOf("Y") + 1);
    }
    
    protected int[] getFrequency(String chain_code) {
        int[] frequency = new int[9];
        
        String code = chain_code.substring(chain_code.indexOf("Y") + 1);
        
        for (int i = 0; i < code.length(); i++) {
            frequency[code.charAt(i) - 49]++;
        }
        
        return frequency;
    }
    
    protected String getSequence(String chain_code) {
        String sequence = "";
        
        String code = chain_code.substring(chain_code.indexOf("Y") + 1);
        
        sequence += code.charAt(0);
        for (int i = 1; i < code.length(); i++) {
            if (sequence.charAt(sequence.length() - 1) != code.charAt(i)) {
                sequence += code.charAt(i);
            }
        }
       
        return sequence;
    }
    
    protected int cekGarisLurus(String code) {
        int garisLurus = 0;
        
        if (code.contains("222222")) {
            garisLurus++;
        }
        if (code.contains("444444")) {
            garisLurus++;
        }
        if (code.contains("666666")) {
            garisLurus++;
        }
        if (code.contains("888888")) {
            garisLurus++;
        }
        
        return garisLurus;
    }
    
    protected int cekGarisMiring(String sequence) {
        int garisMiring = 0;
        
        if (sequence.contains("898989")) {
            garisMiring++;
        }
        if (sequence.contains("323232")) {
            garisMiring++;
        }
        if (sequence.contains("878787")) {
            garisMiring++;
        }
        if (sequence.contains("121212")) {
            garisMiring++;
        }
        if (sequence.contains("696969")) {
            garisMiring++;
        }
        if (sequence.contains("747474")) {
            garisMiring++;
        }
        if (sequence.contains("414141")) {
            garisMiring++;
        }
        if (sequence.contains("636363")) {
            garisMiring++;
        }
        
        return garisMiring;
    }
    
    protected int cekKurvaCembungKanan(String sequence) {
        int kurvaCembungKanan = 0;
        
        String another = sequence;
        
        while(another.indexOf("696969") != -1) {
            another = another.substring(another.indexOf("69"));
            
            while(another.startsWith("69")) {
                another = another.substring(2);
            }
            
            if (another.startsWith("898989")) {
                while(another.startsWith("89")) {
                    another = another.substring(2);
                }
                
                if (another.startsWith("878787")) {
                    while(another.startsWith("87")) {
                        another = another.substring(2);
                    }
                    
                    if (another.startsWith("474747")) {
                        while(another.startsWith("47")) {
                            another = another.substring(2);
                        }
                        
                        kurvaCembungKanan++;
                    }
                }
            }
        }
        
        return kurvaCembungKanan;
    }
    
    protected int cekKurvaCembungKiri(String sequence) {
        int kurvaCembungKiri = 0;
        
        String another = sequence;
        
        while(another.indexOf("414141") != -1) {
            another = another.substring(another.indexOf("41"));
            
            while(another.startsWith("41")) {
                another = another.substring(2);
            }
            
            if (another.startsWith("212121")) {
                while(another.startsWith("21")) {
                    another = another.substring(2);
                }
                
                if (another.startsWith("232323")) {
                    while(another.startsWith("23")) {
                        another = another.substring(2);
                    }
                    
                    if (another.startsWith("636363")) {
                        while(another.startsWith("63")) {
                            another = another.substring(2);
                        }
                        
                        kurvaCembungKiri++;
                    }
                }
            }
        }
        
        return kurvaCembungKiri;
    }

    protected int cekThinnedKurvaCembungKanan(String sequence) {
        int kurvaCembungKanan = 0;
        
        String another = sequence;
        
        while(another.indexOf("6969") != -1) {
            another = another.substring(another.indexOf("69"));
            
            while(another.startsWith("69")) {
                another = another.substring(2);
            }
            
            if (another.startsWith("8989")) {
                while(another.startsWith("89")) {
                    another = another.substring(2);
                }
                
                if (another.startsWith("8787")) {
                    while(another.startsWith("87")) {
                        another = another.substring(2);
                    }
                    
                    if (another.startsWith("4747")) {
                        while(another.startsWith("47")) {
                            another = another.substring(2);
                        }
                        
                        kurvaCembungKanan++;
                    }
                }
            }
        }
        
        return kurvaCembungKanan;
    }
    
    protected int cekThinnedKurvaCembungKiri(String sequence) {
        int kurvaCembungKiri = 0;
        
        String another = sequence;
        
        while(another.indexOf("4141") != -1) {
            another = another.substring(another.indexOf("41"));
            
            while(another.startsWith("41")) {
                another = another.substring(2);
            }
            
            if (another.startsWith("2121")) {
                while(another.startsWith("21")) {
                    another = another.substring(2);
                }
                
                if (another.startsWith("2323")) {
                    while(another.startsWith("23")) {
                        another = another.substring(2);
                    }
                    
                    if (another.startsWith("6363")) {
                        while(another.startsWith("63")) {
                            another = another.substring(2);
                        }
                        
                        kurvaCembungKiri++;
                    }
                }
            }
        }
        
        return kurvaCembungKiri;
    }
    
}
